﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections;
using System.Windows.Media;
using System.Diagnostics;
using System.Collections.Specialized;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    internal class DisconnectedUIElementCollection : UIElementCollection, INotifyCollectionChanged
    {
        public DisconnectedUIElementCollection(UIElement owner) : this(owner, new SurrogateVisualParent())
		{
		
		}

        private DisconnectedUIElementCollection(UIElement owner, SurrogateVisualParent surrogateVisualParent) : base(surrogateVisualParent, null)
        {
            _ownerPanel = owner as Panel;
            _surrogateVisualParent = surrogateVisualParent;
            _surrogateVisualParent.InitializeOwner(this);
            _elements = new Collection<UIElement>();
        }

        public override int Add(UIElement element)
        {
            VerifyWriteAccess();
            return base.Add(element);
        }

        public override int Capacity
        {
            get { return base.Capacity; }
            set
            {
                VerifyWriteAccess();
                base.Capacity = value;
            }
        }

        public override void Clear()
        {
            VerifyWriteAccess();
            base.Clear();
        }

        public override bool Contains(UIElement element)
        {
            return _elements.Contains(element);
        }

        public override void CopyTo(Array array, int index)
        {
            ((ICollection)_elements).CopyTo(array, index);
        }

        public override void CopyTo(UIElement[] array, int index)
        {
            _elements.CopyTo(array, index);
        }

        public override int Count
        {
            get { return _elements.Count; }
        }

        public override IEnumerator GetEnumerator()
        {
            return (_elements as ICollection).GetEnumerator();
        }

        public override int IndexOf(UIElement element)
        {
            return _elements.IndexOf(element);
        }

        public override void Insert(int index, UIElement element)
        {
            VerifyWriteAccess();
            base.Insert(index, element);
        }

        public override void Remove(UIElement element)
        {
            VerifyWriteAccess();
            base.Remove(_degenerateSiblings[element]);
        }

        public override void RemoveAt(int index)
        {
            VerifyWriteAccess();
            base.RemoveAt(index);
        }

        public override void RemoveRange(int index, int count)
        {
            VerifyWriteAccess();
            base.RemoveRange(index, count);
        }

        public override UIElement this[int index]
        {
            get { return _elements[index] as UIElement; }
            set
            {
                VerifyWriteAccess();
                base[index] = value;
            }
        }

        public void Initialize()
        {

        }

        private int BaseIndexOf(UIElement element)
        {
            return base.IndexOf(element);
        }

        private void BaseInsert(int index, UIElement element)
        {
            base.Insert(index, element);
        }

        private void BaseRemoveAt(int index)
        {
            base.RemoveAt(index);
        }

        private UIElement EnsureUIElement(object value)
        {
            if (value == null)
                throw new ArgumentException("Cannot add a null value to a DisconnectedUIElementCollection");

            if (!(value is UIElement))
                throw new ArgumentException("Only objects of type UIElement can be added to a DisconnectedUIElementCollection");

            return value as UIElement;
        }

        private void VerifyWriteAccess()
        {
            if (_ownerPanel == null) return;

            if (_ownerPanel.IsItemsHost && ItemsControl.GetItemsOwner(_ownerPanel) != null)
                throw new InvalidOperationException("Disconnected children cannot be explicitly added to this "
                    + "collection while the panel is serving as an items host. However, visual children can "
                    + "be added by simply calling the AddVisualChild method.");
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        private void RaiseCollectionChanged(NotifyCollectionChangedAction action, object changedItem, int index)
        {
            if (CollectionChanged != null)
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(action, changedItem, index));
        }

        private class DegenerateSibling : UIElement
        {
            public DegenerateSibling(UIElement element)
            {
                _element = element;
            }

            public UIElement Element
            {
                get { return _element; }
            }

            private UIElement _element;
        }

        private class SurrogateVisualParent : UIElement
        {
            internal void InitializeOwner(DisconnectedUIElementCollection owner)
            {
                _owner = owner;
            }

            protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
            {
                if (_internalUpdate) return;

                _internalUpdate = true;
                try
                {
                    if (visualAdded != null)
                    {
                        Debug.Assert(!(visualAdded is DegenerateSibling),
                            "Unexpected addition of degenerate... All degenerates should be added during internal updates.");

                        UIElement element = visualAdded as UIElement;
                        DegenerateSibling sibling = new DegenerateSibling(element);
                        int index = _owner.BaseIndexOf(element);
                        _owner.BaseRemoveAt(index);
                        _owner.BaseInsert(index, sibling);
                        _owner._degenerateSiblings[element] = sibling;
                        _owner._elements.Insert(index, element);
                        _owner.RaiseCollectionChanged(NotifyCollectionChangedAction.Add, element, index);
                    }

                    if (visualRemoved != null)
                    {
                        Debug.Assert(visualRemoved is DegenerateSibling,
                            "Unexpected removal of UIElement... All non degenerates should be removed during internal updates.");

                        DegenerateSibling sibling = visualRemoved as DegenerateSibling;
                        int index = _owner._elements.IndexOf(sibling.Element);
                        _owner._elements.RemoveAt(index);
                        _owner.RaiseCollectionChanged(NotifyCollectionChangedAction.Remove, sibling.Element, index);
                        _owner._degenerateSiblings.Remove(sibling.Element);
                    }
                }
                finally
                {
                    _internalUpdate = false;
                }
            }

            private DisconnectedUIElementCollection _owner;
            private bool _internalUpdate = false;
        }

        private Dictionary<UIElement, DegenerateSibling> _degenerateSiblings = new Dictionary<UIElement, DegenerateSibling>();
        private Collection<UIElement> _elements = new Collection<UIElement>();
        private Panel _ownerPanel;
        private SurrogateVisualParent _surrogateVisualParent;
    }
}